<template>
	<view class="search">
		<musichead title='搜索' :icon='true' :iconblack='true'></musichead>
		<!-- 搜索框 -->
		<view class="search-search">
			<text class="iconfont icon-fangdajing"></text>
			<!-- @confirm 当按下回车时触发函数  @input 当在输入的时候触发 -->
			<input type="text" placeholder="搜索歌曲" v-model="searchWord" @confirm="handleToSearch(searchWord)" @input='handleToSuggest' />
			<text v-show="searchType!==1" class="iconfont icon-guanbi" @tap="handleToDetele" ></text>
		</view>
		<view class="container">

			<scroll-view scroll-y="true">
				<block v-if="searchType==1">
					<!-- 历史记录组件 -->
					<view class="search-history">
						<view class="head">
							<text>历史记录</text>
							<text class="iconfont icon-lajixiang" @tap="handleToDeteHistory"></text>
						</view>
						<view class="list">
							<view v-for="(item,index) in searchHistory" :key='index' @tap="handleToWord(item)">{{item}}
							</view>
						</view>
					</view>
					<!-- 热搜榜布局 -->
					<view class="search-hot">
						<view class="hot-head">热搜榜</view>
						<view class="item" v-for="(item,index) in searchHot" :key='item.searchWord'
							@tap="handleToWord(item.searchWord)">
							<view class="top">{{index+1}}</view>
							<view class="word">
								<view>
									{{item.searchWord}}
									<image :src="item.iconUrl" mode="aspectFit"></image>
								</view>
								<view class="buchong">{{item.content}}</view>
							</view>
							<text class="count">{{item.score}}</text>
						</view>
					</view>
				</block>
				<!-- 搜索结果 -->
				<block v-else-if="searchType==2">
					<view class="search-result">
						<view class="result-item" v-for="(item,index) in searchList" :key='item.id' @tap="handleToMusic(item.id)">
							<view class="item-word">
								<view class="word1">{{item.name}}</view>
								<view class="word2">{{item.artists[0].name}} - {{item.album.name}}</view>
							</view>
							<text class="iconfont icon-bofang"></text>
						</view>
					</view>
				</block>
				<!-- 搜索建议 -->
				<block v-else-if="searchType==3">
					<view class="search-suggest" >
						<view class="suggest-head">
							搜索‘{{searchWord}}’
						</view>
						<view class="suggest-item" v-for="(item,index) in searchSuggest" :key='index+10' @tap="handleToWord(item.keyword)">
							<text class="iconfont icon-fangdajing"></text>
							{{item.keyword}}
						</view>
					</view>
				</block>
			</scroll-view>
		</view>


	</view>
</template>

<script>
	import musichead from '@/components/musichead/musichead.vue'
	import {
		searchHot,
		searchWord,
		searchSuggest
	} from '@/common/api.js'
	export default {
		data() {
			return {
				searchHot: [],
				searchWord: '',
				searchHistory: [],
				searchType: 1,
				//保存搜索的结果
				searchList:[],
				searchSuggest:[]
			};
		},
		onLoad() {
			searchHot().then((res) => {
				if (res[1].data.code == '200') {
					this.searchHot = res[1].data.data
				}

			})

			//将本地存储的历史记录保存到searchHistory中
			uni.getStorage({
				key: 'searchHistory',
				success: (res) => {
					this.searchHistory = res.data
				}
			});
		},
		methods: {
			handleToWord(word) {
				this.searchWord = word
				this.handleToSearch(word)
			},
			handleToSearch(word) {
				//从头部添加
				this.searchHistory.unshift(word)
				console.log(this.searchHistory);
				//数组去重，重复的不能添加
				this.searchHistory = [...new Set(this.searchHistory)]
				//数组保持最大10个数据
				if (this.searchHistory.length > 10) {
					this.searchHistory.length = 10
				}
				//将搜索历史保存到本地存储中
				uni.setStorage({
					key: 'searchHistory',
					data: this.searchHistory

				})
				this.getSearchList(word)
			},
			handleToDetele() {
				this.searchWord = '',
				this.searchType=1
			},
			handleToDeteHistory() {
				uni.removeStorage({
					key: 'searchHistory',
					success: (res) => {
						this.searchHistory = []
					}
				})
			},
			//获取搜索的数据
			getSearchList(word) {
				searchWord(word).then((res) => {
					console.log(res);
					if(res[1].data.code=='200') {
						this.searchList=res[1].data.result.songs
						this.searchType=2
					}
				})
			},
			//跳转到歌曲详情 
			handleToMusic(id) {
				uni.navigateTo({
					url:'/pages/detail/detail?songId=' + id
				})
			},
			//输入时触发的搜索建议
			handleToSuggest(event) {
				let value=event.detail.value  //通过事件的detail.value可以拿到当前正在输入的值
				console.log(value);
				if(!value) {
					this.searchType=1
					return 
				}
				searchSuggest(value).then(res => {
					if(res[1].data.code=='200') {
						this.searchSuggest=res[1].data.result.allMatch
						console.log(this.searchSuggest);
						this.searchType=3
					}
				})
			}
		},
		components: {
			musichead
		}
	}
</script>

<style lang="less" scoped>
	.search {
		.search-search {
			display: flex;
			align-items: center;
			height: 80rpx;
			margin: 10rpx 30rpx 30rpx 30rpx;
			background: #f7f7f7;
			border-radius: 50rpx;

			text {
				font-size: 26rpx;
				margin-left: 28rpx;
				margin-right: 26rpx;
			}

			input {
				font-size: 28rpx;
				flex: 1;
			}


		}

		.search-history {
			margin: 0 30rpx;
			font-size: 26rpx;

			.head {
				display: flex;
				justify-content: space-between;
				margin-bottom: 36rpx;
			}

			.list {
				display: flex;
				//折行
				flex-wrap: wrap;

				view {
					padding: 16rpx 28rpx;
					border-radius: 40rpx;
					margin: 0 30rpx 30rpx 0;
					background: #f7f7f7;
				}
			}
		}

		.search-hot {
			margin: 0 30rpx;
			font-size: 26rpx;

			.hot-head {
				margin-top: 30rpx;
			}

			.item {
				display: flex;
				//上下对齐
				align-items: center;
				margin-bottom: 58rpx;
				margin-top: 35rpx;

				.top {
					color: #fb2222;
					width: 60rpx;
					margin-left: 8rpx;
				}

				.word {
					font-size: 30rpx;
					flex: 1;


					image {
						width: 48rpx;
						height: 22rpx;
						margin-left: 4rpx;

					}

					.buchong {
						font-size: 24rpx;
						color: #878787;
						width: 60vw;
						/* 只在一行显示，多出省略 */
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
				}

				.count {
					color: #878787;
				}
			}

		}
		
		.search-result {
			border-top: 2rpx #e4e4e4 solid;
			padding: 30rpx;
			
			.result-item {
				display: flex;
				justify-content: space-between;
				align-content: center;
				padding-bottom: 30rpx;
				margin-bottom: 30rpx;
				border-bottom: 2rpx #e4e4e4 solid;
				.word1 {
					font-size: 28rpx;
					color: #235790;
					margin-bottom: 12rpx;
					
				}
				.word2 {
					font-size: 22rpx;
					color: #898989;
				}
				
				text {
					font-size: 50rpx;
				}
			}
		}
		
		.search-suggest {
			border-top: 2px #e4e4e4 solid;
			padding: 30rpx;
			
			.suggest-head {
				color: #4574a5;
				margin-bottom: 74rpx;
				
			}
			.suggest-item {
				color: #5d5d5d;
				margin-bottom: 74rpx;
				
				text {
					color: #bdbdbd;
					margin-right: 28rpx;
				}
			}
		}
	}
</style>
